package logic

import (
	"context"
	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"
	"edu-pro/models/mysql"
	"edu-pro/pkg"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type EvaluationAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewEvaluationAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EvaluationAddLogic {
	return &EvaluationAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *EvaluationAddLogic) EvaluationAdd(in *edurpc.EvaluationAddRequest) (*edurpc.EvaluationAddResponse, error) {
	// 添加评价
	if len(in.Content) > 500 {
		return nil, errors.New("文字评价在500字以内")
	}
	if pkg.CheckWord(in.Content) {
		in.Status = 1
		return nil, errors.New("含违规词汇，请修改")
	}
	eval := mysql.Evaluation{
		CourseId:    int(in.CourseId),
		UserId:      int(in.UserId),
		Score:       int(in.Score),
		Content:     in.Content,
		IsAnonymous: int(in.IsAnonymous),
		Status:      int(in.Status),
	}
	mysql.DB.Debug().Create(&eval)
	return &edurpc.EvaluationAddResponse{
		EvalId: int64(eval.ID),
	}, nil
}
